<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$data = array(
				'action' => 'home'
		);
		
		$this->title = "Cong Ty TNHH Ngoc Thanh Toan";
		$this->load->view('//home/index', $data);
		
	}
	
	public function intro()
	{
		$data = array(
					'action' => 'intro'
				);
		$this->title = "Cong Ty TNHH Ngoc Thanh Toan";
		$this->load->view('//home/intro', $data);
	}
	
	public function product($cat_alias)
	{
		$data = array(
				'action' => 'product'
		);
		$this->title = "Cong Ty TNHH Ngoc Thanh Toan";
		
		$categories = $this->db->query("SELECT c.* FROM category c ORDER BY c.id ASC")->result();
		$dataC = array();
		
		if (count($categories) > 0) {
			foreach($categories as $cat) {
				$items = $this->db->query("SELECT i.* FROM item i WHERE i.category_id = ".$cat->id)->result();
				$dataC[$cat->name] = $items;
			}
		}
		 
		$posts = $this->db->query("SELECT p.* FROM post p INNER JOIN item i ON p.item_id = i.id WHERE i.alias = '".$cat_alias."' ORDER BY p.id ASC")->result();
		
		/*$em = $this->getDoctrine()->getManager();
		$categories = $em->getRepository('NttCoreBundle:Category')->getCategoryList();
		$data = array();
		if (count($categories) > 0) {
			foreach ($categories as $cat) {
				$items = $em->getRepository('NttCoreBundle:Item')->getItemByCategoryId($cat['id']);
				$data[$cat['name']] = $items;
			}
		}
		 
		$posts = $this->get('knp_paginator')->paginate($em->getRepository('NttCoreBundle:Post')->getPostByItemAlias($alias), $this->get('request')->query->get('page', 1), self::MAX_SIZE_PER_PAGE);
		 
		return $this->render('NttHomepageBundle:Default:product.html.twig',
				array('active' => 'product',
						'data' => $data,
						'posts' => $posts,
						'alias' => $alias,
						'pager' => $posts));*/
		
		$data['data'] = $dataC;
		$data['posts'] = $posts;
		$data['alias'] = $cat_alias;
		
		$this->load->view('//home/product', $data);
	}
        
        public function shopping()
	{
		$data = array('action' => 'shopping');
		$this->title = "Shopping - Cong Ty TNHH Ngoc Thanh Toan";
		$this->load->view('//home/shopping', $data);
	}
}
